package avicit.bdp.dgs.mdm.rest;

import avicit.bdp.dgs.mdm.dto.TableDTO;
import avicit.bdp.dgs.mdm.service.TableService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-12-04
 * @类说明：TableRest
 * @修改记录：
 * @注意事项：
 * @主要功能：模型Rest接口
 */
@RestController
@Api(tags = "mdm", description = "模型Rest接口")
@RequestMapping("/api/bdp/dgs/mdm/table")
public class TableRest {
    @Autowired
    private TableService tableService;

    /**
     * 新增模型
     *
     * @param tableDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增模型")
    public ResponseMsg<String> addModel(@RequestBody TableDTO tableDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.addModel(tableDTO));

        return responseMsg;
    }

    /**
     * 删除/批量删除模型，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除模型 ，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteModel(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.deleteModel(id));

        return responseMsg;
    }

    /**
     * 修改模型
     *
     * @param tableDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改模型")
    public ResponseMsg<Boolean> updateModel(@RequestBody TableDTO tableDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.updateModel(tableDTO));

        return responseMsg;
    }

    /**
     * 查询模型详情
     *
     * @param id 模型ID
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查询模型详情")
    public ResponseMsg<TableDTO> getModelDetail(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<TableDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.getModelDetail(id));

        return responseMsg;
    }

    /**
     * 分页查询模型
     *
     * @param topicId
     * @param keyWords
     * @param code
     * @param status
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "分页查询模型")
    public ResponseMsg<QueryRespBean<TableDTO>> queryModel(
            @ApiParam(value = "主题ID", name = "topicId") @RequestParam(required = false) String topicId,
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "编码", name = "code") @RequestParam(required = false) String code,
            @ApiParam(value = "状态", name = "status") @RequestParam(required = false) Integer status,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<TableDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.queryModel(topicId, keyWords, code, status, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 发布模型
     *
     * @param id
     * @return
     */
    @PostMapping("/publish")
    @ApiOperation(value = "发布模型")
    public ResponseMsg<Boolean> publishModel(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.publishModel(id));

        return responseMsg;
    }

    /**
     * 启用/停用
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "启用/停用")
    @PostMapping("/updateStatus")
    public ResponseMsg<Boolean> updateStatus(@ApiParam(value = "模型ID", name = "id") @RequestParam(required = false) String id,
                                             @ApiParam(value = "状态", name = "status") @RequestParam(required = false) Integer status) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.updateStatus(id, status));

        return responseMsg;
    }

    /**
     * 修订
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "修订")
    @PostMapping("/revise")
    public ResponseMsg<Boolean> reviseTable(@ApiParam(value = "模型ID", name = "id") @RequestParam(required = false) String id) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.reviseTable(id));

        return responseMsg;
    }

    /**
     * 模型名称或编码是否重复
     *
     * @param id   模型ID，规则编辑时修改名称，需要传入modelId;新增场景不需要传入
     * @param name 名称
     * @param code 编码
     * @return
     */
    @GetMapping("/isSameName")
    @ApiOperation(value = "模型名称或编码是否重复")
    public ResponseMsg<Boolean> isSameName(@ApiParam(value = "ID", name = "id") @RequestParam(required = false) String id,
                                           @ApiParam(value = "名称", name = "name") @RequestParam(required = false) String name,
                                           @ApiParam(value = "编码", name = "code") @RequestParam(required = false) String code) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.tableService.isSameName(id, name, code));

        return responseMsg;
    }
}
